const TOOLBAR =[
	{content:"批量处理", icon:"el-icon-magic-stick",type:"primary"}
]
const COLUMNS = [
	{prop:"createdatetime", label:"日期",width:"150"},
	{prop:"content", label:"通知内容",width:"250"},
	{prop:"memo", label:"处理说明",width:"250"},
	{prop:"type", label:"类别",width:"100"},
	{prop:"status", label:"状态",width:"80"},
	{prop:"trackername", label:"跟踪者",width:"80"},
	{prop:"updatetime", label:"处理日期",width:"150"},
	{prop:"presser", label:"处理者",width:"80"},
]

let validate = {
    validatePass: (rule, value, callback) => {
        
    }
};

const notifyRules = {
    memo: [
        {required: true, message: '请输处理说明', trigger: 'blur'}
    ]
};
			
let app = new Vue({
    el: "#app",
    data() {
        return {
        	leftbar_show:true,
			// 添加、编辑标识
			tableHeight:window.innerHeight - 180,
			toolbar:TOOLBAR,
			tablecolums:COLUMNS,
			dialogFormVisible:false,
			dialogbatchFormVisible:false,
			queryForm:{
				userid: localStorage.getItem('USERID'),
                keyword: "",
                type: [],
				status:[],
				createdatetime:[],
				pageNo:1, // 当前页码
				pagesize:100 ,// 每页记录数
				pagesizes:[10,20,50,100,500]
				
			},
			pagetotal:0,
			pickerOptions: {
			  shortcuts: [{
				text: '最近一周',
				onClick(picker) {
				  const end = new Date()
				  const start = new Date()
				  start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
				  picker.$emit('pick', [start, end])
				}
			  }, {
				text: '最近一个月',
				onClick(picker) {
				  const end = new Date();
				  const start = new Date();
				  start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
				  picker.$emit('pick', [start, end]);
				}
			  }, {
				text: '最近三个月',
				onClick(picker) {
				  const end = new Date();
				  const start = new Date();
				  start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
				  picker.$emit('pick', [start, end]);
				}
			  }]
			},
			notifyRules, // 数据验证规则
			notifyForm: {
                id: null,
                memo: "",
				presser:localStorage.getItem('USERID')
            },
			batchForm:{
				memo:""
			},
			// 多选
			multipleSelection:[],
			pagedata:[],  // 当前页结果集
			loaded:false,
        }
    },
	methods: {
		// 显示侧边栏
		showLeftBar() {
			if (this.leftbar_show) {
				this.leftbar_show = false
			} else {
				this.leftbar_show = true
			}
		},
		
		// 表格多选
		handleSelectionChange(val) {
			this.multipleSelection = val;
		},
		selectionChange(selection){
			this.selection = selection
		},
		toolAction(acName) {
			if (acName == "批量处理") {
				this.batch()
			} 
		},
		// 批量处理
		batch(index, row) {
			if (this.multipleSelection.length > 0) {
				for (let row of this.multipleSelection) {
					if (row.status != "未处理") {
						this.$message.error('请选择未处理的数据行！');
						return;
					}
				}
				this.dialogbatchFormVisible = true;
			} else {
				this.$message.error('请选择未处理的数据行！');
			}
		},
		// 确认批量处理
		async cofirmBatch(){
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let ids =[]
				for (let row of this.multipleSelection) {
					ids.push(row.id)
				}
				if (this.batchForm.memo == "") {
					this.$message.error("处理内容不能为空。");
				} else {
					let param = {"memo":this.batchForm.memo, "ids":ids,"presserid":localStorage.getItem('USERID')}
					let res = await axios.put('api/notify/batch', param);
					if (res.code === 200) {
						this.$message.success(res.message);
						this.query();
						this.dialogbatchFormVisible = false
					}else {
						this.$message.error(res.message);
					}					
				}
		
			}catch (e) {
				this.$message.error('批量处理请求出错！', e.message);
			}
			// 关闭加载框
			loading.close();
		},
		// 确认
		async cofirm(){
            this.$refs.notifyForm.validate(async valid => {
                if (valid){
					// 显示加载框
					const loading = this.$loading(LOAD_STYLE);
					try {
						let res = null;
						// 编辑
						res = await axios.put('api/notify/update', this.notifyForm);
						if (res.code === 200) {
							this.query();
							this.dialogFormVisible = false
						}else {
							this.$message.error(res.message);
						}					

					}catch (e) {
						this.$message.error('请求出错！', e.message);
					}
					// 关闭加载框
					loading.close();
                }
            })			
		},
		start(){
			this.queryForm.pageNo = 1
			this.query()
		},
		// 查询
		async query() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.post('api/notify/query', this.queryForm);
				if (res.code === 200) {
					this.pagedata = res.data;
					this.pagetotal = res.total;
					this.$message.success(res.message);
				}else {
					this.$message.error(res.message);
				}
			}catch (e) {
				this.$message.error('请求出错！', e.message);
			}
			
			// 关闭加载框
			loading.close();
		},
		// 编辑
		edit(index, row) {
			this.index = index;
			this.dialogFormVisible = true;
			this.notifyForm = deepCopy(row)
		},
		// 设定表格当前页
		handleCurrentPageChange (selectedPageNo) {
		  this.queryForm.pageNo = selectedPageNo
		  // 加载数据页
		  this.query()
		},
	},
	async mounted() {
		// 自动调整表格高度
		window.onresize = () => {
		  return (() => {
			this.tableHeight = window.innerHeight - 180
		  })()
		}
	
	}

});	